--
-- Author: yawei.zhu
-- Date: 2018-10-29 16:49:25
--
-- DynastyMiniMapPanel
--

local ClassRef = app.mvc.createPanelClass(...)

local panelWidth = 560
local mapWidth = 14000 -- 除去左右边界各500的距离
local scaleX = panelWidth / mapWidth

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.DynastyMiniMapScene))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)
    self._teamNodes = {}
    self._ui.player:setLocalZOrder(2) -- 把自己提到最上层
    self._ui.player:findChild("Panel/star"):setVisible(true)
    self:refresh()
end

function ClassRef:refresh()
    if not self._data then return end
end

function ClassRef:updateView(townInfo, myGuildId)
    if townInfo.defer.guildId ~= myGuildId then
        self._ui.bg:setScaleX(-1)
    end

    -- 初始化一次 队伍人数
    self:setMyTeamCount(app:sendMsg("TeamModel", "getMemberNum"))
end

function ClassRef:updateCharcterPosX(posX)
    posX = posX - 500 -- 减去右边界500的距离
    local targetX = posX * scaleX
    if targetX < 0 then targetX = 0 end
    if targetX > panelWidth then targetX = panelWidth end
    self._ui.player:setPositionX(targetX)
end

function ClassRef:updateNodePosX(node, posX)
    posX = posX - 500 -- 减去右边界500的距离
    local targetX = posX * scaleX
    if targetX < 0 then targetX = 0 end
    if targetX > panelWidth then targetX = panelWidth end
    node:setPositionX(targetX)
end

function ClassRef:updateOtherCharcterPosX(uid, posX)
   if not self._teamNodes[uid] then return end

   local data = self._teamNodes[uid]
   self:updateNodePosX(data.node, posX)
end

function ClassRef:setMyTeamCount(num)
    self._ui.player:findChild("Panel/num_b"):setString(num)
end

function ClassRef:removeAllTeamNodes()
    for _, data in pairs(self._teamNodes) do
        local node = data.node
        node:removeFromParent()
    end
    self._teamNodes = {}
end

function ClassRef:insertOtherTeamMember(leaderId, isEnemy, posX)
    local data = self._teamNodes[leaderId]
    if not data then
        local node = cc.CSLoader:createNode(Res.DynastyMiniMapIcon)
        node:setPosition(cc.p(0, 12))
        self._ui.Panel:addChild(node)
        data = {count = 0, node = node}
        self._teamNodes[leaderId] = data
    end

    local node = data.node
    data.count = data.count + 1

    if isEnemy then
        node:findChild("Panel/bg_b"):setVisible(false)
        node:findChild("Panel/bg_r"):setVisible(true)
        node:findChild("Panel/num_b"):setVisible(false)
        node:findChild("Panel/num_r"):setVisible(true)

        node:findChild("Panel/num_r"):setString(data.count)
    else
        node:findChild("Panel/num_b"):setString(data.count)
    end

    if posX then
        self:updateNodePosX(node, posX)
    end
end

function ClassRef:onOperate(cb)
    self._onOperate = cb
end

return ClassRef
